package com.yibo.message.mq;

import com.yibo.db.message.entity.EmailServerEntity;
import com.yibo.message.Helper.SendEmail;
import com.yibo.message.core.EmailMessage;
import com.yibo.message.service.EmailService;
import com.yibo.support.helper.Assert;
import com.yibo.support.mq.YBProcess;
import com.yibo.support.pojo.YBMessage;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import static com.yibo.support.constant.MsgConstant.EMAIL_SERVER_NOT_FOUND;
import static com.yibo.support.context.SpringContext.getBean;

@Component
@EnableBinding(YBProcess.class)
public class MessageInput  {

    @StreamListener(YBProcess.YB_INPUT)
    public void  input(Message<YBMessage> messageMessage){
        try {
            EmailServerEntity emailServerEntity = getBean(EmailService.class).emailService();
            Assert.notEmpty(EMAIL_SERVER_NOT_FOUND, emailServerEntity);

            EmailMessage message = new EmailMessage();
            message.setTitle(messageMessage.getPayload().getTitle());
            message.setMessage(messageMessage.getPayload().getMessage());
            message.setEmail(messageMessage.getPayload().getEmail());
            SendEmail.send(emailServerEntity, message);
            System.out.println("接受消息："+message.getMessage());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
